/**
 * 
 */
package com.shine.icis.dao.imp;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;

import com.shine.icis.dao.IDataSubmitDAO;
import com.shine.icis.dao.imp.BaseHibernateDAO;
import com.shine.icis.dto.ReturnMsg;

/**
 * @author zsy
 *
 */
public class DataSubmitDAO extends BaseHibernateDAO implements IDataSubmitDAO {
	
	@SuppressWarnings("deprecation")
	@Override
	public ReturnMsg initSubmitTables(String proSql, int beginDate, int endDate, String increment) {
		try {
			CallableStatement proc = getSession().connection().prepareCall(proSql);
			proc.setInt(1, 0);
			proc.setInt(2, beginDate);
			proc.setInt(3, endDate);
			proc.setString(4, increment);
			proc.setInt(5, 0);
			proc.registerOutParameter(6, Types.VARCHAR);
			proc.registerOutParameter(7, Types.INTEGER);
			
			proc.execute();
		    String msg = proc.getString(6);
		    int code = proc.getInt(7);
		    proc.close();
		    return new ReturnMsg(code, msg);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new HibernateException(e);
		}
		
	}
	
//	public void initSubmitTables(String proc, int beginDate, int endDate, boolean increment) {
//		class ProWork implements Work {
//			private int beginDate;
//			private int endDate;
//			private String incrementStr;
//			private String procSql;
//			ProWork(int beginDate, int endDate, String incrementStr, String procSql) {
//				this.beginDate = beginDate;
//				this.endDate = endDate;
//				this.incrementStr = incrementStr;
//				this.procSql = procSql;
//			}
//			@Override
//			public void execute(Connection connection) throws SQLException {
//				CallableStatement proc = connection.prepareCall(procSql);
//				proc.setInt(1, 0);
//				proc.setInt(2, beginDate);
//				proc.setInt(3, endDate);
//				proc.setString(4, incrementStr);
//				proc.setInt(5, 0);
//				proc.registerOutParameter(6, Types.VARCHAR);
//				proc.registerOutParameter(7, Types.INTEGER);
//				
//				proc.execute();
//			    String msg = proc.getString(6);
//			    int code = proc.getInt(7);
//			    if (code != 0) {
//			    	throw new SQLException(msg);
//			    }
//			}
//		}
//		Work proWrok = new ProWork(beginDate, endDate, (increment ? "1" : "0"), proc);
//		//执行work 
//		//doWork 在3.3以上版本才支持
//		getSession().doWork(proWrok);
//	}
	
	/*
	@SuppressWarnings("unchecked")
	@Override
	public List<Object> findSQLData(String sql, List<Object> params, int pageNo, int pageSize) {
    	if (sql == null) {
    		throw new IllegalArgumentException();
    	}
    	//query.addScalar()设置顺序会影响结果集字段的顺序
    	//通过别名的在SQl串中的位置来排序
    	 
    	TreeSet<Scalar> scalars = new TreeSet<Scalar>();
    	//有as s_[number]时（不区分大小写）转为String类型
		Pattern pattern = Pattern.compile("as\\s+(s_[\\d]+)", Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(sql);
		while(matcher.find()) {
			String name = matcher.group(1);
			scalars.add(new Scalar(name, Hibernate.STRING, sql.indexOf(name)));
		}
		//有as l_[number]时（不区分大小写）转为Long类型
		pattern = Pattern.compile("as\\s+(l_[\\d]+)", Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(sql);
		while(matcher.find()) {
			String name = matcher.group(1);
			scalars.add(new Scalar(name, Hibernate.LONG, sql.indexOf(name)));
		}
		//有as d_[number]时（不区分大小写）转为Double类型
		pattern = Pattern.compile("as\\s+(d_[\\d]+)", Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(sql);
		while(matcher.find()) {
			String name = matcher.group(1);
			scalars.add(new Scalar(name, Hibernate.DOUBLE, sql.indexOf(name)));
		}
    	
    	
    	
        int firstResultIndex = (pageNo - 1) * pageSize;

    	SQLQuery query = getSession().createSQLQuery(sql);
//		//有as s_[number]时（不区分大小写）转为String类型
//		Pattern pattern = Pattern.compile("as\\s+(s_[\\d]+)", Pattern.CASE_INSENSITIVE);
//		Matcher matcher = pattern.matcher(sql);
//		while(matcher.find()) {
//			query.addScalar(matcher.group(1), Hibernate.STRING);
//		}
//		//有as l_[number]时（不区分大小写）转为Long类型
//		pattern = Pattern.compile("as\\s+(l_[\\d]+)", Pattern.CASE_INSENSITIVE);
//		matcher = pattern.matcher(sql);
//		while(matcher.find()) {
//			query.addScalar(matcher.group(1), Hibernate.LONG);
//		}
//		//有as d_[number]时（不区分大小写）转为Double类型
//		pattern = Pattern.compile("as\\s+(d_[\\d]+)", Pattern.CASE_INSENSITIVE);
//		matcher = pattern.matcher(sql);
//		while(matcher.find()) {
//			query.addScalar(matcher.group(1), Hibernate.DOUBLE);
//		}
    	for (Scalar scalar : scalars) {
    		query.addScalar(scalar.name, scalar.type);
    	}
		if (params != null) {
			for (int i = 0; i < params.size(); i++) {
				query.setParameter(i, params.get(i));
			}
		}
      	query.setFirstResult(firstResultIndex);
      	if (pageSize > 0) {
      		query.setMaxResults(pageSize);
      	}
          	
      	return query.list();
    }
	
	
	private class Scalar implements Comparable<Scalar> {
		String name;
		NullableType type;
		int order;
		Scalar(String name, NullableType type, int order) {
			this.name = name;
			this.type = type;
			this.order = order;
		}
		@Override
		public int compareTo(Scalar o) {
			if (order == o.order) {
				return 0;
			}
			return order > o.order ? 1 : -1;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			final Scalar other = (Scalar) obj;
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			return true;
		}
		
	}
	*/
}
